package jgenerator;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.*;


@SuppressWarnings("serial")
public class GeneratorGUI extends JFrame{
	private final static int MAX_ITEMS_PER_LINE = 3;
	int nbItemsGenerator;
	// Fields to input the token
	JTextField[] token;
	JTextField tokenEndOfLine;
	JTextField iterationMin, iterationMax;
	
	
	JCheckBox addSpaces;//Checkbox allowing to add or not the spaces
	
	// Generate button
	JButton generate;
	
	//The area which gets the generated text
	JTextArea generatedText;
	
	
	
	Generator generator;
	
	
	public GeneratorGUI(int nbBlocs){
		super();
		this.nbItemsGenerator = nbBlocs;
		initComponents();
	}


	private void initComponents() {
		/** Configuration des composants **/
		setContentPane(buildContentPane());
		
	/** Configuration de la fenetre **/
		setTitle("AutoGenerate");	//Application title

		Toolkit tk = Toolkit.getDefaultToolkit();
	    Dimension screenSize = tk.getScreenSize();
	    int screenHeight = screenSize.height;
	    int screenWidth = screenSize.width;
	    setSize(screenWidth / 2, screenHeight / 2);
	    setLocation(screenWidth / 4, screenHeight / 4);

		setResizable(false);		//
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
	}
	
	
	private JPanel buildContentPane(){
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout(3,1));
		
		//Creation and customization of the panels;
			JPanel panelTokensInput;
				/** Next line allows to display properly the appli 
				 * 	indeed users may input big numbers 
				 * 	and the appli may become very ugly... 
				 */
				int nbLines = (int) Math.ceil((double)nbItemsGenerator/MAX_ITEMS_PER_LINE);
				panelTokensInput = new JPanel(new GridLayout(nbLines,1));
			JPanel panelChampsAutre = new JPanel();
			JPanel panelTexteGenere   = new JPanel();
			
		
		//Initialisation of the token array
			token = new JTextField[nbItemsGenerator];
				//System.out.println(modeleGenerateur.length);
		
			for(int i = 0;i<token.length;i++){
				token[i] = new JTextField();
				token[i].setColumns(15);
					token[i].setToolTipText(i+"e token");
			 		panelTokensInput.add(token[i]);
			}
				
		JLabel tokenEndOfLineLabel = new JLabel("End of line token");
		tokenEndOfLine = new JTextField();
			tokenEndOfLine.setColumns(10);
		JLabel iterationMinLabel = new JLabel("Min = ");
		iterationMin = new JTextField();
			iterationMin.setColumns(5);
		JLabel iterationMaxLabel = new JLabel("Max = ");
		iterationMax = new JTextField();
			iterationMax.setColumns(5);
		
		addSpaces = new JCheckBox("Add spaces");
		
		generate  = new JButton(new GenerateAction(this, "Generate"));
			
		
		panelChampsAutre.add(tokenEndOfLineLabel);
		panelChampsAutre.add(tokenEndOfLine);
		panelChampsAutre.add(iterationMinLabel);
		panelChampsAutre.add(iterationMin);
		panelChampsAutre.add(iterationMaxLabel); 
		panelChampsAutre.add(iterationMax);
		panelChampsAutre.add(addSpaces);
		panelChampsAutre.add(generate);
		
		
		generatedText = new JTextArea(5,30);
			generatedText.setEditable(false);
		JScrollPane scrollPaneGeneratedText = new JScrollPane(generatedText);
		panelTexteGenere.add(scrollPaneGeneratedText);
		
		
		//Ajout des panels au panel master
		panel.add(panelTokensInput,BorderLayout.PAGE_START);
		panel.add(panelChampsAutre,BorderLayout.CENTER);
		panel.add(panelTexteGenere,BorderLayout.PAGE_END);
		return panel;
	}
	
	
	/******************************************************
	 * ****************************************************
	 *  			Getters and setters					  *
	 ******************************************************
	 ******************************************************
	
	/**
	 * @return the modeleGenerateurDebut
	 */
	public JTextField[] getModeleGenerateur() {
		return token;
	}


	/**
	 * @param modeleGenerateurDebut the modeleGenerateurDebut to set
	 */
	public void setModeleGenerateur(JTextField[] modeleGenerateur) {
		this.token= modeleGenerateur;
	}

	public JTextField getModeleGenerateurAtPosition(int i){
		return token[i];
	}
	/**
	 * @return the debutIteration
	 */
	public JTextField getDebutIteration() {
		return iterationMin;
	}


	/**
	 * @param debutIteration the debutIteration to set
	 */
	public void setDebutIteration(JTextField debutIteration) {
		this.iterationMin = debutIteration;
	}


	/**
	 * @return the finIteration
	 */
	public JTextField getFinIteration() {
		return iterationMax;
	}


	/**
	 * @param finIteration the finIteration to set
	 */
	public void setFinIteration(JTextField finIteration) {
		this.iterationMax = finIteration;
	}


	/**
	 * @return the texteGenere
	 */
	public JTextArea getTexteGenere() {
		return generatedText;
	}


	/**
	 * @param texteGenere the texteGenere to set
	 */
	public void setTexteGenere(JTextArea texteGenere) {
		this.generatedText = texteGenere;
	}
	
	public JCheckBox getAjouterEspace(){
		return addSpaces;
	}


	/**
	 * @return the finDeLigne
	 */
	public JTextField getFinDeLigne() {
		return tokenEndOfLine;
	}


	/**
	 * @param finDeLigne the finDeLigne to set
	 */
	public void setFinDeLigne(JTextField finDeLigne) {
		this.tokenEndOfLine = finDeLigne;
	}

}

